package com.college.controller.products;


import com.college.common.pojo.ao.DeliveryAddress;
import com.college.common.pojo.dto.products.AddDeliveryAddressDTO;
import com.college.common.pojo.dto.products.QueryDeliveryAddressDTO;
import com.college.common.pojo.dto.products.UpdateDeliveryAddressDTO;
import com.college.common.pojo.model.page.PageParamRequest;
import com.college.common.response.CommonPage;
import com.college.common.response.CommonResult;
import com.college.common.utils.object.ObjectUtil;
import com.college.products.service.DeliveryAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货地址 前端控制器
 * @author 小C工作室
 * @since 2025-02-22
 */
@RestController
@RequestMapping("/delivery-address")
@Api(tags = "收货地址")
@Slf4j
public class DeliveryAddressController {

    @Autowired
    private DeliveryAddressService deliveryAddressService;

    @ApiOperation(value = "查询收货地址列表")
    @GetMapping(value = "/query-list")
    public CommonResult<List<DeliveryAddress>> queryList(QueryDeliveryAddressDTO queryDeliveryAddressDTO) {
        return CommonResult.success(deliveryAddressService.queryList(queryDeliveryAddressDTO));
    }

    @ApiOperation(value = "查询收货地址列表分页")
    @GetMapping(value = "/query-list-page")
    public CommonResult<CommonPage<DeliveryAddress>> queryListPage(QueryDeliveryAddressDTO queryDeliveryAddressDTO , PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(deliveryAddressService.queryListPage(queryDeliveryAddressDTO,pageParamRequest)));
    }

    @ApiOperation(value = "新增收货地址")
    @PostMapping(value = "/add")
    public CommonResult<String> addDeliveryAddress(@RequestBody AddDeliveryAddressDTO addDeliveryAddressDTO) {
        return deliveryAddressService.addDeliveryAddress(addDeliveryAddressDTO);
    }

    @ApiOperation(value = "删除收货地址")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResult<Boolean> deleteDeliveryAddress(@PathVariable Long id) {
        return CommonResult.success(deliveryAddressService.removeById(id));
    }

    @ApiOperation(value = "批量删除收货地址")
    @DeleteMapping(value = "/batch-delete")
    public CommonResult<Boolean> deleteDeliveryAddress(@RequestBody List<Long> ids) {
        if (ObjectUtil.isNotEmpty(ids)) {
            return CommonResult.success(deliveryAddressService.removeByIds(ids));
        }
        return CommonResult.success("参数错误");
    }

    @ApiOperation(value = "更新收货地址")
    @PutMapping(value = "/update")
    public CommonResult<String> updateDeliveryAddress(@RequestBody UpdateDeliveryAddressDTO updateDeliveryAddressDTO) {
        return deliveryAddressService.updateDeliveryAddress(updateDeliveryAddressDTO);
    }
}

